package com.slmn.visitor.gui;

import java.util.ArrayList;
import java.util.List;

import com.slmn.visitor.gui.Component.CNodeState;
import com.slmn.visitor.gui.Component.HChild;

import javafx.geometry.Bounds;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class FormulaFrame extends StackPane {
	public static boolean isSelecting = false;
	final private static HComposite root = new HComposite();
	final public static Rectangle rect = new Rectangle();
	final public static FormulaFrame ff = new FormulaFrame();
	final private static List<HChild> selected = new ArrayList<>();

	static {
		rect.setFill(Color.web("blue", 0.1));
		rect.setStroke(Color.BLUE);
		rect.setArcHeight(10);
		rect.setArcWidth(10);
		rect.setManaged(false);
	}

	private FormulaFrame() {
		super();
		this.getChildren().addAll(root.getNode());
		// HComposite OnMouseClicked can't clear isSelecting when dragged
		// outside HComposite, here as safeguard.
		this.setOnMouseClicked(mEvent -> {
			FormulaFrame.isSelecting = false;
			clearSelected();
		});

	}

	public static void clear() {
		root.clear();
	}

	public static void add(Component c) {
		root.addAll(c);
	}

	public static void clearSelected() {
		for (HChild hc : selected) {
			hc.setCNodesBG(CNodeState.CLEAR);
		}
		selected.clear();
	}

	public static void addSelected(HChild hc) {
		hc.setCNodesBG(CNodeState.SELECTED);
		selected.add(hc);
	}
	
	public static Bounds getRectBounds(){
		return rect.localToScene(rect.getLayoutBounds());
	}

}
